<?php

namespace App\Admin\Actions\Staff;

use App\Library\Logic\OperateLogLogic;
use App\Models\AdminUsersModel;
use App\Models\ShopUserModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;

class StaffDeleteAction extends RowAction
{
    public $name = '删除';

    public function handle(ShopUserModel $model): Response
    {
        $model->user_delete_flag = 1;
        $model->delete();
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.staff.delete');
        $adminInfo = AdminUsersModel::query()->where('user_id', $model->user_id)->first();
        if (!empty($adminInfo)) {
            DB::table('admin_role_users')->where('user_id', $adminInfo->id)->delete();
            $adminInfo->delete();
        }
        return $this->response()->success('操作成功')->refresh();
    }
}
